package com.cwj.mansysback.servlet.impl;

import com.cwj.mansysback.service.api.CourseService;
import com.cwj.mansysback.service.api.ElectiveService;
import com.cwj.mansysback.service.api.StudentService;
import com.cwj.mansysback.service.api.TeacherService;
import com.cwj.mansysback.service.impl.CourseServiceImpl;
import com.cwj.mansysback.service.impl.ElectiveServiceImpl;
import com.cwj.mansysback.service.impl.StudentServiceImpl;
import com.cwj.mansysback.service.impl.TeacherServiceImpl;
import com.cwj.mansysback.servlet.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DeleteAccServlet extends HttpServlet {

    private static final String accKeys[] = {"Admin", "Teacher", "Stud"};
    private static final TeacherService teaSer = new TeacherServiceImpl();
    private static final CourseService courseSer = new CourseServiceImpl();

    private static final StudentService stuSer = new StudentServiceImpl();

    private static final ElectiveService eleSer = new ElectiveServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("------------------ DeleteAccServlet start ------------------");
        String id = req.getParameter("aid");

        if (id == null) {
            MyUtils.writeResponse(resp, new Statement(8));
            return;
        }

        Statement statement = new Statement(8);

        if (req.getRequestURI().contains(DeleteAccServlet.accKeys[1])) {
            if (courseSer.selectCourseTea(id).size() > 0) {
                statement.setStatue(10);
            } else {
                if (!teaSer.deleteTeacher(id)) statement = new Statement(11);
                else statement.setStatue(0);
            }
        } else if (req.getRequestURI().contains(DeleteAccServlet.accKeys[2])) {

            if (eleSer.selectPackBySid(id).getCount() > 0) {
                statement.setStatue(10);
            } else {
                if(! stuSer.deleteStudent(id)){
                    statement.setStatue(11);
                }else statement.setStatue(0);
            }

        }

        MyUtils.writeResponse(resp, statement);

        System.out.println("------------------ DeleteAccServlet end ------------------");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
